package com.tqjc.system.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tqjc.system.common.entity.VO.AlarmInfoVO;
import com.tqjc.system.common.entity.VO.StatDataVO;
import com.tqjc.system.common.enums.AlarmStatusEnum;
import com.tqjc.system.system.dao.SysAlarmInfoDao;
import com.tqjc.system.system.entity.SysAlarmInfoEntity;
import com.tqjc.system.system.service.SysAlarmInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @Author: 86195
 * @Date: 2024/1/3 16:30
 **/
@Service
@Slf4j
public class SysAlarmInfoServiceImpl extends ServiceImpl<SysAlarmInfoDao, SysAlarmInfoEntity> implements SysAlarmInfoService {
    @Override
    public IPage<SysAlarmInfoEntity> getListByPage(Page<SysAlarmInfoEntity> pageEntity, AlarmInfoVO alarmInfoVO) {
        LambdaQueryWrapper<SysAlarmInfoEntity> queryWrapper = new LambdaQueryWrapper<SysAlarmInfoEntity>()
                .eq(StringUtils.isNotEmpty(alarmInfoVO.getCarName()), SysAlarmInfoEntity::getCarName, alarmInfoVO.getCarName())
                .le(alarmInfoVO.getHappenEndDate() != null, SysAlarmInfoEntity::getCreateDate, alarmInfoVO.getHappenEndDate())
                .ge(alarmInfoVO.getHappenStartDate() != null, SysAlarmInfoEntity::getCreateDate, alarmInfoVO.getHappenStartDate())
                .eq(alarmInfoVO.getStatus() != null,SysAlarmInfoEntity::getStatus,alarmInfoVO.getStatus())
                .in(CollectionUtil.isNotEmpty(alarmInfoVO.getStatusList()),SysAlarmInfoEntity::getStatus,alarmInfoVO.getStatusList())
                .orderByDesc(SysAlarmInfoEntity::getCreateDate);
        return super.page(pageEntity,queryWrapper);
    }

    @Override
    public int isNewAlarmInfo(String carName, String type) {
        return baseMapper.selectCount(new LambdaQueryWrapper<SysAlarmInfoEntity>().ge(SysAlarmInfoEntity::getCreateDate, LocalDateTime.now().minusDays(7))
                .eq(SysAlarmInfoEntity::getCarName,carName).eq(SysAlarmInfoEntity::getType,type)
                .in(SysAlarmInfoEntity::getStatus, AlarmStatusEnum.ALARM_STATUS_INIT.getStatus()
                        ,AlarmStatusEnum.ALARM_STATUS_DOING.getStatus(),AlarmStatusEnum.ALARM_STATUS_ERROR.getStatus()));
    }

    @Override
    public List<StatDataVO> getStatData(AlarmInfoVO alarmInfoVO) {
        return baseMapper.getStatData(alarmInfoVO);
    }
}
